﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        public Form4(int id, string tible, string author, string createdtime)
        {
            InitializeComponent();
            this.id = id;
            txttible.Text = tible.ToString();
            txtauthor.Text = author.ToString();
            txtcreatedtime.Text = createdtime.ToString();

        }
        private int id;
        private void button1_Click(object sender, EventArgs e)
        {
            var tible = txttible.Text.ToString();
            var author = txtauthor.Text.ToString();
            var createdtime = txtcreatedtime.Text.ToString();
            //确定保存


            if (id > 0)
            {
                if (!string.IsNullOrEmpty(tible))
                {

                    var sql = string.Format("update  Articles set Tible={0},Author={1},CreatedTime={2} where id={3} ", tible, author, createdtime, this.id);
                    var res = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("更新成功", "提示");

                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            else
            {


                if (!string.IsNullOrEmpty(tible))
                {

                    var sql = string.Format("insert into Articles(Tible,Author,CreatedTime) values('{0}','{1}','{2}')", tible, author, createdtime);
                    var res = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }

                }

                else
                {
                    MessageBox.Show("作品名字不能为空");
                    
                }
            }

            
        }

                private void button2_Click(object sender, EventArgs e)
                {
                    //取消
                    this.DialogResult = DialogResult.No;
                    this.Close();
                }

                private void txtUserName_TextChanged(object sender, EventArgs e)
                {

                }

                private void Form4_Load(object sender, EventArgs e)
                {

                }
            }
        }

